package cn.iocoder.yudao.module.system.repository.mybatis.dao.logger;

import cn.iocoder.yudao.common.pojo.PageResult;
import cn.iocoder.yudao.core.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.core.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum;
import cn.iocoder.yudao.module.system.repository.mybatis.domain.logger.LoginLog;
import cn.iocoder.yudao.module.system.vo.logger.loginlog.LoginLogExportReqVO;
import cn.iocoder.yudao.module.system.vo.logger.loginlog.LoginLogPageReqVO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface LoginLogMapper extends BaseMapperX<LoginLog> {

    default PageResult<LoginLog> selectPage(LoginLogPageReqVO reqVO) {
        LambdaQueryWrapperX<LoginLog> query = new LambdaQueryWrapperX<LoginLog>()
                .likeIfPresent(LoginLog::getUserIp, reqVO.getUserIp())
                .likeIfPresent(LoginLog::getUsername, reqVO.getUsername())
                .betweenIfPresent(LoginLog::getCreateTime, reqVO.getCreateTime());
        if (Boolean.TRUE.equals(reqVO.getStatus())) {
            query.eq(LoginLog::getResult, LoginResultEnum.SUCCESS.getResult());
        } else if (Boolean.FALSE.equals(reqVO.getStatus())) {
            query.gt(LoginLog::getResult, LoginResultEnum.SUCCESS.getResult());
        }
        query.orderByDesc(LoginLog::getId); // 降序
        return selectPage(reqVO, query);
    }

    default List<LoginLog> selectList(LoginLogExportReqVO reqVO) {
        LambdaQueryWrapperX<LoginLog> query = new LambdaQueryWrapperX<LoginLog>()
                .likeIfPresent(LoginLog::getUserIp, reqVO.getUserIp())
                .likeIfPresent(LoginLog::getUsername, reqVO.getUsername())
                .betweenIfPresent(LoginLog::getCreateTime, reqVO.getCreateTime());
        if (Boolean.TRUE.equals(reqVO.getStatus())) {
            query.eq(LoginLog::getResult, LoginResultEnum.SUCCESS.getResult());
        } else if (Boolean.FALSE.equals(reqVO.getStatus())) {
            query.gt(LoginLog::getResult, LoginResultEnum.SUCCESS.getResult());
        }
        query.orderByDesc(LoginLog::getId); // 降序
        return selectList(query);
    }

}
